package cn.eveo.refreshandloadmore.view;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Created by Administrator on 2016/9/23.
 */

public class EListView extends ListView {

    protected interface LoadMoreListener {
        void onLoadMore();
    }

    private LoadMoreListener loadMoreListener;
    private OnScrollListener onScrollListener;
    private boolean loadMoreEnabled = false;
    private LoadMoreView loadMoreView;


    public EListView(Context context) {
        super(context);
        initView();
    }

    public EListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public EListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        loadMoreView = new LoadMoreView(getContext());
        loadMoreView.setLayoutParams(new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        addFooterView(loadMoreView);
        setOnScrollListener(null);
        loadMoreView.setState(LoadMoreView.STATE_NOT_ENABLED);
        loadMoreView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public void setOnScrollListener(OnScrollListener l) {
        this.onScrollListener = l;
        super.setOnScrollListener(new WrapScrollListener());
    }

    /**
     * 是否显示加载更多项
     **/
    protected void setLoadingMoreEnabled(boolean enabled) {
        loadMoreEnabled = enabled;
    }

    /**
     * 监听加载
     **/
    protected void setLoadMoreListener(LoadMoreListener loadMoreListener) {
        this.loadMoreListener = loadMoreListener;
    }

    protected void dismissLoadMoreView() {
        if (null != loadMoreView) {
            loadMoreView.setState(LoadMoreView.STATE_NOT_ENABLED);
        }
    }

    /**
     * 加载完成
     **/
    protected void loadMoreComplete() {
        if (null == loadMoreView || !loadMoreEnabled || null == getAdapter()) {
            return;
        }
        if (getAdapter().getCount() <= 1) {
            loadMoreView.setState(LoadMoreView.STATE_NOT_ENABLED);
        } else {
            loadMoreView.setState(LoadMoreView.STATE_COMPLETE);
        }
    }

    /**
     * 无更多数据
     **/
    protected void noMore() {
        if (null == loadMoreView || !loadMoreEnabled) {
            return;
        }
        loadMoreView.setState(LoadMoreView.STATE_NOMORE);
    }

    private class WrapScrollListener implements OnScrollListener {
        private int mLastItemIndex;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (null != onScrollListener) {
                onScrollListener.onScrollStateChanged(view, scrollState);
            }

            if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                    && mLastItemIndex == view.getAdapter().getCount() && !canScrollVertically(1)
                    && loadMoreEnabled) {

                loadMoreView.setState(LoadMoreView.STATE_LOADING);
                if (null != loadMoreListener) {
                    loadMoreListener.onLoadMore();
                }
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (null != onScrollListener) {
                onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
            mLastItemIndex = firstVisibleItem + visibleItemCount;
        }
    }

    @Override
    public void setAdapter(final ListAdapter adapter) {
        super.setAdapter(adapter);
        adapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                if (adapter.getCount() <=1 && null != loadMoreView) {
                    loadMoreView.setState(LoadMoreView.STATE_NOT_ENABLED);
                }
            }
        });
    }
}
